<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="供应商" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="yfpayList">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="单号" align="center" prop="yfCode" />
      <el-table-column label="供应商" align="center" prop="supplyName" />
      <el-table-column
        label="总数量"
        align="center"
        prop="zsum"
      ></el-table-column>
      <el-table-column label="总金额" align="center" prop="zprice" />
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="审核状态" align="center" prop="isSh" width="100">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isSh === '0'">已通过</el-tag>
          <el-tag type="info" v-if="scope.row.isSh === '1'">待审核</el-tag>
          <el-tag type="danger" v-if="scope.row.isSh === '2'">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSh(scope.row)"
             v-hasPermi="['supermarket:xqpurchase:list']"
            >查看</el-button
          >
           <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['supermarket:yfpay:Fzd']"
            @click="handleFzd(scope.row)"
            >反制单</el-button
          >
        </template>
      </el-table-column>
    </el-table>
 <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改应付汇总对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-table
        v-loading="loading"
        :data="xqyfpayList"
        show-summary
        :summary-method="getSummaries"
      >
        <el-table-column type="index" label="序号" align="center" />
        <el-table-column label="采购单号" align="center" prop="purchaseCode" />
        <el-table-column label="商品码" align="center" prop="productcode" />
        <el-table-column label="商品名称" align="center" prop="productName" />
        <el-table-column label="规格" align="center" prop="unit" />
        <el-table-column label="数量" align="center" prop="sum" />
        <el-table-column label="价格" align="center" prop="price" />
        <el-table-column label="总金额" align="center" prop="zprice" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listYfpayFzd, updateFzd } from "@/api/supermarket/yfpay";
import {
  listXqyfpay,
  getXqyfpay,
  delXqyfpay,
  addXqyfpay,
  updateXqyfpay,
} from "@/api/supermarket/xqyfpay";
export default {
  name: "Yfpay",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应付汇总表格数据
      yfpayList: [],
      xqyfpayList: [],
      // 弹出层标题
      title: "",
      yspayId: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplyId: null,
        zprice: null,
        createName: null,
        isSh: null,
        shName: null,
        shTime: null,
        payTime: null,
        invoice: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询应付汇总列表 */
    getList() {
      this.loading = true;
      listYfpayFzd(this.queryParams).then((response) => {
        this.yfpayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListXq(row) {
      const data = {
        yspayId: row.yfpayId,
      };
      listXqyfpay(data).then((res) => {
        this.xqyfpayList = res.data;
      });
    },
    //详情列表
    handleSh(row) {
      this.title =
        "详情：" +
        row.yfCode +
        ">创建人:" +
        row.createName +
        ">供应商:" +
        row.supplyName;
      this.getListXq(row);
      this.yspayId = row.yfpayId;
      this.open = true;
    },
    handleFzd(row) {
      this.form.yfpayId = row.yfpayId;
      updateFzd(this.form).then(res => {
        this.$modal.msgSuccess("反制单成功");
        this.getList();
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        yfpayId: null,
        supplyId: null,
        zprice: null,
        createTime: null,
        createName: null,
        isSh: null,
        shName: null,
        shTime: null,
        payTime: null,
        invoice: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
        getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (index === 6) {
          sums[index] = "";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
  },
};
</script>
